/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.advrubberband;

import edu.idp.client.interfaces.Fillable;
import edu.idp.shared.StoredObject;

import java.awt.*;

/**
 * A Rubberband that does rectangles.
 *
 * @author Kristopher T Babic
 */
public class RubberbandRectangle extends Rubberband implements Fillable { //, Storable{
    private boolean fill = false;
    private boolean line = true;

    public RubberbandRectangle() {
    }

    public RubberbandRectangle(Component component) {
        super(component);
    }

    public void setFill(boolean b) {
        if (!b && !line)
            line = true;
        fill = b;
    }

    public void setLine(boolean b) {
        if (!b)
            fill = true;
        line = b;
    }

    public void drawEnd(Graphics graphics) {
        Rectangle r = getBounds();
        graphics.setColor(getColor());

        if (fill) {
            if (line) {
                graphics.fillRect(r.x + 1, r.y + 1, r.width - 1, r.height - 1);
                graphics.setColor(Color.black);
                graphics.drawRect(r.x, r.y, r.width, r.height);
            }
            else {
                graphics.fillRect(r.x, r.y, r.width + 1, r.height + 1);
            }
        }
        else {
            graphics.drawRect(r.x, r.y, r.width, r.height);
        }
    }

    public void drawLast(Graphics graphics) {
        Rectangle rect = lastBounds();
        graphics.drawRect(rect.x, rect.y,
                          rect.width, rect.height);
    }

    public void drawNext(Graphics graphics) {
        Rectangle rect = getBounds();
        graphics.drawRect(rect.x, rect.y,
                          rect.width, rect.height);
    }

    /*

     public LinkedList store() {

         LinkedList S = new LinkedList();

         S.add(this.getType());
         S.add(this.getColor());
         S.add(new Boolean(fill));
         S.add(new Boolean(line));
         S.add(new Point(anchorPt));
         S.add(new Point(endPt));

         return S;

     }

     public void restore(LinkedList V) {

         Graphics g = getCompGraphic();
         Graphics g2 = getGraphic();

         this.setColor((Color)V.get(1));
         Boolean f = (Boolean)V.get(2);
         Boolean l = (Boolean)V.get(3);
         setFill(f.booleanValue());
         setLine(l.booleanValue());
         anchorPt = new Point((Point)V.get(4));
         endPt = new Point((Point)V.get(5));
         stretchedPt = new Point((Point)V.get(5));
         drawEnd(g);
         drawEnd(g2);

         g.dispose();
         g2.dispose();

     }
     */
    protected String storeExtra() {

        String S = new String();
        char c = (char) 0;

        if (fill)
            c |= 2;

        if (line)
            c |= 1;

        //	S.add(new Boolean(fill));
        //	S.add(new Boolean(line));
        S += c;
        return S;
    }

    protected void restoreExtra(String S) {
        try {
            char c = S.charAt(0);

            if ((c & 2) != 0)
                fill = true;
            else
                fill = false;

            if ((c & 1) != 0)
                line = true;
            else
                line = false;

        }
        catch (Exception e) {
        }
    }

    public int getType() {
        return StoredObject.RECTANGLE;
    } //DrawType.RECTANGLE; }
}
